package com.huizhonglm.diap.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.shop.ShopInfo;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.utils.DialogUtil;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.user.MyCollectionPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.shop.ShopDetailActivity;
import com.huizhonglm.diap.ui.user.iView.MyCollectionIView;
import com.huizhonglm.diap.widget.LoadView;
import com.bruce.pullrefresh.PullToRefreshBase;
import com.bruce.pullrefresh.PullToRefreshListView;

import java.util.List;

/**
 * 我的收藏
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-22 13:42
 */
public class MyCollectionActivity extends BaseActivity implements MyCollectionIView,
        AdapterView.OnItemClickListener, LoadView.OnLoadAgainListener,
        PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemLongClickListener {

    private PullToRefreshListView pullView;
    private ListView mListView;
    private LoadView loadView;

    private List<ShopInfo> mData;
    private MyCollectionAdapter mAdapter;
    private MyCollectionPresenter mPresenter;

    private String userID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection);
        setBackEvent(this, null);
        userID = UserUtil.getUserId();
        mPresenter = new MyCollectionPresenter(this);
        assignViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        onLoadAgain();
    }

    @Override
    protected void assignViews() {
        loadView = (LoadView) findViewById(R.id.load_view);
        pullView = (PullToRefreshListView) findViewById(R.id.my_collection_list);
        pullView.setMode(PullToRefreshBase.Mode.BOTH);
        pullView.setOnRefreshListener(this);
        mListView = pullView.getRefreshableView();
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
        loadView.setDataView(pullView, this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ShopInfo shop = (ShopInfo) parent.getAdapter().getItem(position);
        if (shop != null) {
            Intent intent = new Intent(this, ShopDetailActivity.class);
            intent.putExtra("shopId", shop.getSid());
            startActivity(intent);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        ShopInfo shop = (ShopInfo) parent.getAdapter().getItem(position);
        if (shop != null) {
            cancelDialog(shop.getSid());
        }
        return true;
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mPresenter.loadData(Constants.LOAD_DROP, userID);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        mPresenter.loadData(Constants.LOAD_MORE, userID);
    }

    @Override
    public void onLoadAgain() {
        loadView.showProgress();
        mPresenter.loadData(Constants.LOAD_INIT, userID);
    }

    @Override
    public void onLoadDataSuccess(String pullFlag, List<ShopInfo> data) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                this.mData = data;
                updatePage(data);
                loadView.showDataView();
                break;
            case Constants.LOAD_DROP:
                this.mData = data;
                updatePage(data);
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                this.mData.addAll(data);
                updatePage(data);
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onLoadDataFailure(String pullFlag) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                loadView.showErrorView();
                break;
            case Constants.LOAD_DROP:
                ToastUtil.makeTextShort("刷新失败");
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                ToastUtil.makeTextShort("加载更多失败");
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onLoadDataEmpty(String pullFlag) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                loadView.showEmptyView(null);
                break;
            case Constants.LOAD_DROP:
                ToastUtil.makeTextShort("列表已经是最新");
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                ToastUtil.makeTextShort("到底部啦");
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onCancelSuccess() {
        hideProgressBar();
        ToastUtil.makeTextShort("取消收藏成功");
        onLoadAgain();
    }

    @Override
    public void onCancelFailure() {
        hideProgressBar();
        ToastUtil.makeTextShort("取消收藏失败，请稍后重试");
    }

    private void cancelDialog(final String shopId){
        DialogUtil.showNormalDialog(this, "取消收藏", "您确认要取消此收藏吗？", "取消", "确定", new DialogUtil.OnDialogBtnClickListener() {
            @Override
            public void onBtnClick(String btnText) {
                if ("确定".equals(btnText)) {
                    showProgressBar();
                    mPresenter.cancelCollect(shopId);
                }
            }
        });
    }

    private void updatePage(List<ShopInfo> data){
        setPullViewMode(data);
        if (mAdapter == null) {
            mAdapter = new MyCollectionAdapter(getLayoutInflater(), mData);
            mListView.setAdapter(mAdapter);
        } else {
            mAdapter.setData(mData);
            mAdapter.notifyDataSetChanged();
        }
    }

    private void setPullViewMode(List<ShopInfo> data){
        if (!EmptyUtil.isEmpty(data)) {
            if (mData.size() >= Constants.PAGE_SIZE) {
                if (data.size() < Constants.PAGE_SIZE) {
                    pullView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    pullView.setMode(PullToRefreshBase.Mode.BOTH);
                }
            } else {
                pullView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
        }
    }
}
